package routing;

import geocoding.CustomNodes;
import android.location.Location;
import android.util.Log;

public class Compass {
	
	public String getOrientation (Location thisLocation, Location checkLocation){
		
		float bearing = thisLocation.bearingTo(checkLocation);
		
		String orientation=" ";
		
		if ((-157.5 < bearing) && (bearing < -112.5)){
			orientation="Südwesten";
		}
		
		if ((-112.5 < bearing) && (bearing < -67.5)){
			orientation="Westen";
		}
		
		if ((-67.5 < bearing) && (bearing < -22.5)){
			orientation="Nordwesten";
		}
		
		if ((-22.5 < bearing) && (bearing < 22.5)){
			orientation="Norden";
		}
		
		if ((22.5 < bearing) && (bearing < 67.5)){
			orientation="Nordosten";
		}
		if ((67.5 < bearing) && (bearing < 112.5)){
			orientation="Osten";
		}
		if ((112.5 < bearing) && (bearing < 157.5)){
			orientation="Südosten";
		}
		
		if ((157.5 < bearing ) || (bearing < -157.5)){
			orientation="Süden";
		}
		return orientation;
	}
	
	public float convertBearings (float bearing) {
		float cBearing = bearing;
		
		if (bearing < -180) {
			cBearing = 360+bearing;
		}
		
		if (bearing > 180) {
			cBearing = bearing - 360;
		}
		
		
		return cBearing;
	}
	
	
//	Checks if a Landmark is within a 180 (-90, +90) degree Angle from current Location to Target
	
	public boolean correctAngle90 (float btT, float btLm) {
		boolean isCorrect=false;
		
		float leftBorder = convertBearings(btT-90);
		float rightBorder = convertBearings(btT+90);
		
		if (btT < -90 || btT > 90) {
			
			if (btLm < rightBorder || btLm > leftBorder) {
				isCorrect=true; 
			}
			
		}
		
		else {
			
			if (btLm > leftBorder && btLm < rightBorder) {
				isCorrect=true; 
			}
			
		}
		
		return isCorrect;
	}
	
	public String getDirection (Location thisLocation, int lmID, int nextNodeID, CustomNodes cn) {
		String TAG="URNav";
		String direction="empty";
		
		Location lmLocation = new Location("x");
		lmLocation.setLatitude(cn.getDlati(lmID));
		lmLocation.setLongitude(cn.getDlongi(lmID));
		
		Location nextLocation = new Location("x");
		nextLocation.setLatitude(cn.getDlati(nextNodeID));
		nextLocation.setLongitude(cn.getDlongi(nextNodeID));
		
		float lmBearing = thisLocation.bearingTo(lmLocation);
		float nextBearing = thisLocation.bearingTo(nextLocation);
		
		if  (((nextBearing < -90) && (lmBearing > 0 )) || ((nextBearing > 90) && (lmBearing < 0))) {
			if (nextBearing > lmBearing) {
				direction = "Links";
			}
			
			else {  
				direction = "Rechts";
			}
		}
		
		else {
			if (nextBearing > lmBearing) {
				direction = "Rechts";
			}
			
			else {
				direction = "Links";
			}
		}
	return direction;	
	}


	
}
